package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.emtity.Category;
import com.itheima.reggie.emtity.Dish;
import com.itheima.reggie.emtity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/*
/**
 * 根据id删除分类，删除之前需要进行判断
 * @param id
 */

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    //当前模块调用过了其他模块的sevice 对象
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;



    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Dish::getCategoryId,id);
        //SELECT COUNT(id) FROM dish WHERE category_id='xxx'
        int count1 = dishService.count(lqw1);
        //如果已经关联菜品,抛出业务异常
        if(count1 > 0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }


        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(lqw2);
        //如果已经关联套餐,抛出业务异常
        if(count2 > 0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //正常删除分类
        super.removeById(id);
    }
}
